#include "string.h"
#include "app_manager.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "task_manager.h"

#define TAG "TASK_MANAGER"

TaskHandle_t MediaTask_Handler;
TaskHandle_t LvglTask_Handler;
TaskHandle_t MainTask_Handler;
TaskHandle_t AppTask_Handler;

void sys_bt_task(void *pvParameters);
void sys_main_task(void *pvParameters);
void sys_app_task(void *pvParameters);

const task_info_t task_info[] = {
    {sys_bt_task, BT_SVC_TASK_NAME, BT_SVC_TASK_STACK_SIZE, BT_SVC_TASK_PRIORITY, &LvglTask_Handler, BT_SVC_TASK_CORE},
    {sys_main_task, MAIN_TASK_NAME, MAIN_TASK_STACK_SIZE, MAIN_TASK_PRIORITY, &MainTask_Handler, MAIN_TASK_CORE},
    {sys_app_task, APP_TASK_NAME, APP_TASK_STACK_SIZE, APP_TASK_PRIORITY, &AppTask_Handler, APP_TASK_CORE},
};

TaskHandle_t* task_manager_get_handle(const char *name)
{
    for (int i = 0; i < sizeof(task_info) / sizeof(task_info_t); i++) {
        if (strcmp(task_info[i].task_name, name) == 0) {
            return task_info[i].task_handle;
        }
    }
    return NULL;
}

void task_manager_init(void)
{
    for (int i = 0; i < sizeof(task_info) / sizeof(task_info_t); i++) {
        xTaskCreatePinnedToCore(task_info[i].task_func, task_info[i].task_name, task_info[i].stack_size, 
            NULL, task_info[i].priority, task_info[i].task_handle, task_info[i].core_id);
    }
}

